#include "qruntimedata.h"

#include "quixml.h"

#include <QDateTime>

QStringList QRuntimeData::m_typelist=QStringList()<<"Integer"<<"Float"<<"Boolean"<<"String"<<"Date"<<"Time"<<"DateTime";
QRuntimeData::QRuntimeData():
    m_type("Integer"),
    m_name(""),
    m_holdOnExit(false),
    m_save(false),
    m_saveTime(3600),
    m_category("Default")
{
    initValue();
}

QRuntimeData::~QRuntimeData()
{
}

void QRuntimeData::initValue()
{
    if(m_type=="Integer")
    {
        int v=0;
        m_value.setValue(v);
    }
    else if(m_type=="Float")
    {
        float v=0;
        m_value.setValue(v);
    }
    else if(m_type=="DateTime")
    {
        m_value.setValue(QDateTime::currentDateTime());
    }
    else if(m_type=="String")
    {
        m_value.setValue(QString(""));
    }
    else if(m_type=="Boolean")
    {
        m_value.setValue(true);
    }
    else if(m_type=="Time")
    {
        m_value.setValue(QTime::currentTime());
    }
    else if(m_type=="Date")
    {
        m_value.setValue(QDate::currentDate());
    }
}

bool QRuntimeData::isMemery()
{
    if(m_type=="Integer" || m_type=="Float" || m_type=="Boolean" || m_type=="String")
    {
        return true;
    }
    else
    {
        return false;
    }
}

void QRuntimeData::setValue(const QVariant &value)
{
    if(value.canConvert(m_value.type()))
    {
        m_value=value;
    }
}

QVariant QRuntimeData::value()
{
    return m_value;
}

QString QRuntimeData::valueText()
{
    if(m_type=="Date")
    {
        return m_value.toDate().toString(Qt::LocalDate);
    }
    else if(m_type=="Time")
    {
        return m_value.toTime().toString(Qt::LocalDate);
    }
    else if(m_type=="DateTime")
    {
        return m_value.toDateTime().toString(Qt::LocalDate);
    }
    else if(m_type=="Boolean")
    {
        return m_value.toBool()?"True":"False";
    }
    else if(m_type=="Float")
    {
        return QString::number(m_value.toFloat());
    }
    else if(m_type=="Integer")
    {
        return QString::number(m_value.toInt());
    }
    else
    {
        return m_value.toString();
    }
}

QRuntimeData &QRuntimeData::operator =(const QRuntimeData &data)
{
    m_type=data.m_type;
    m_name=data.m_name;
    m_holdOnExit=data.m_holdOnExit;
    m_save=data.m_save;
    m_saveTime=data.m_saveTime;
    m_value=data.m_value;
    m_category=data.m_category;
    m_inforation=data.m_inforation;

    return *this;
}

void QRuntimeData::setName(const QString &name)
{
    m_name=name;
}

QString QRuntimeData::name()
{
    return m_name;
}

QString QRuntimeData::type()
{
    return m_type;
}

void QRuntimeData::setType(const QString &type)
{
    if(m_type==type)
    {
        return;
    }
    QString vs=valueText();
    m_type=type;
    QVariant v=m_value;
    initValue();
    if(m_type=="String")
    {
        m_value=vs;
    }
    else
    {
        if(v.canConvert(m_value.type()))
        {
            //m_value.setValue(v);
        }
    }
}

void QRuntimeData::setHoldOnExit(bool hold)
{
    m_holdOnExit=hold;
}

bool QRuntimeData::holdOnExit()
{
    return m_holdOnExit;
}

void QRuntimeData::setNeedSave(bool save)
{
    m_save=save;
}

bool QRuntimeData::needSave()
{
    return m_save;
}

void QRuntimeData::setSaveTime(int time)
{
    m_saveTime=time;
}

int QRuntimeData::saveTime()
{
    return m_saveTime;
}

void QRuntimeData::setCategory(const QString &category)
{
    m_category=category;
}

QString QRuntimeData::category()
{
    return m_category;
}

void QRuntimeData::setInformation(const QString &information)
{
    m_inforation=information;
}

QString QRuntimeData::information()
{
    return m_inforation;
}

void QRuntimeData::read(QXmlStreamReader &r)
{
    foreach(const QXmlStreamAttribute &attribute,r.attributes())
    {
        if(attribute.name().toString()=="name")
        {
            m_name=attribute.value().toString();
        }
        else if(attribute.name().toString()=="type")
        {
            m_type=attribute.value().toString();
        }
        else if(attribute.name().toString()=="holdOnExit")
        {
            m_holdOnExit=(attribute.value().toString()=="True");
        }
        else if(attribute.name().toString()=="save")
        {
            m_save=(attribute.value().toString()=="True");
        }
        else if(attribute.name().toString()=="saveTime")
        {
            m_saveTime=attribute.value().toString().toInt();
        }
        else if(attribute.name().toString()=="category")
        {
            m_category=attribute.value().toString();
        }
        else if(attribute.name().toString()=="information")
        {
            m_inforation=attribute.value().toString();
        }
    }
    initValue();
    for(bool finished=false;!finished && !r.hasError();)
    {
        switch(r.readNext())
        {
        case QXmlStreamReader::StartElement:
        {
            XMLValue *v=getXmlValue();
            if(v!=NULL)
            {
                v->read(r);
                m_value.setValue(v->value());
                delete v;
            }
        }
            break;
        case QXmlStreamReader::EndElement:
            finished=true;
            break;
        default:
            break;
        }
    }

}

void QRuntimeData::write(QXmlStreamWriter &w)
{
    w.writeStartElement("","runtimeData");

    w.writeAttribute("name",m_name);
    w.writeAttribute("type",m_type);
    w.writeAttribute("holdOnExit",m_holdOnExit?"True":"False");
    w.writeAttribute("save",m_save?"True":"False");
    w.writeAttribute("saveTime",QString::number(m_saveTime));
    w.writeAttribute("category",m_category);
    w.writeAttribute("information",m_inforation);
    XMLValue *v=getXmlValue();
    if(v!=NULL)
    {
        v->setValue(m_value);
        v->write(w);
        delete v;
    }
    else
    {
        w.writeStartElement("","unknow");
        w.writeEndElement();
    }
    w.writeEndElement();
}

XMLValue* QRuntimeData::getXmlValue()
{
    if(m_type=="Integer")
    {
        return new XMLIntValue;
    }
    else if(m_type=="Float")
    {
        return new XMLFloatValue;
    }
    else if(m_type=="String")
    {
        return new XMLStringValue;
    }
    else if(m_type=="Boolean")
    {
        return new XMLBoolValue;
    }
    else if(m_type=="Date")
    {
        return new XMLDateValue;
    }
    else if(m_type=="Time")
    {
        return new XMLTimeValue;
    }
    else if(m_type=="DateTime")
    {
        return new XMLDateTimeValue;
    }
    return NULL;
}

QStringList QRuntimeData::typeList()
{
    return m_typelist;
}
